package algorithm;

public class leetcode_all_13 {


    public static void main(String[] args) {
//        int result = test("MCMXCIV");
        int result = test("IX");
        System.out.println(result);

    }

    public static int test(String s){

        int result=0;
        char[] chars=s.toCharArray();
        int[] arr=new int[chars.length];
        for(int i=0;i<chars.length;i++){
            if (chars[i]=='I'){
                arr[i]=1;
                continue;
            }
            if (chars[i]=='V'){
                arr[i]=5;
                continue;
            }
            if (chars[i]=='X'){
                arr[i]=10;
                continue;
            }
            if (chars[i]=='L'){
                arr[i]=50;
                continue;
            }
            if (chars[i]=='C'){
                arr[i]=100;
                continue;
            }
            if (chars[i]=='D'){
                arr[i]=500;
                continue;
            }
            if (chars[i]=='M'){
                arr[i]=1000;
                continue;
            }
        }
        for (int i=0;i<arr.length;i++){
            if (i+1<arr.length){
                if (arr[i]<arr[i+1]){  //异常，判断是否特殊情况
                    if(arr[i+1]-arr[i]==4){
                        result+=4;
                    }
                    if(arr[i+1]-arr[i]==9){
                        result+=9;
                    }
                    if(arr[i+1]-arr[i]==40){
                        result+=40;
                    }
                    if(arr[i+1]-arr[i]==90){
                        result+=90;
                    }
                    if(arr[i+1]-arr[i]==400){
                        result+=400;
                    }
                    if(arr[i+1]-arr[i]==900){
                        result+=900;
                    }
                    i++;
                    continue;
                }else{  //正常
                    result+=arr[i];
                }
            }else if (i+1==arr.length){
                result+=arr[i];
            }
        }
        return result;
    }


}
